// mircoApp/business-district/home/home.js
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
var app = getApp();
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    identity: "",
    info: {},
    activeName: 0,
    dataList: [],
    diy_name: "--",

    finished: false,
    isLoadMore:true,

    shopPop: false,
    agentPop: false,
    selectAgent: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initPage();
    this.getData();
  },

  initPage(){
    this.pageOptions = {
      'agent': {
        api: "plugin.business-district.frontend.controllers.agent.index",
        name: "区域代理"
      },
      'promoter': {
        api: "plugin.business-district.frontend.controllers.promoter.index",
        name: "推广者"
      },
      'store': {
        api: "plugin.business-district.frontend.controllers.store.index",
        name: "门店"
      },
      'clerk': {
        api: "plugin.business-district.frontend.controllers.clerk.index",
        name: "店员"
      },
      'platform': {
        api: "plugin.business-district.frontend.controllers.platform.index",
        name: "平台"
      },
      'parent': {
        api: "plugin.business-district.frontend.controllers.parent.index",
        name: ""
      }
    };
    this.setData({
      identity: this.options.identity
    });
    this.activeOptions = this.pageOptions[this.options.identity];
  },
  init() {
    this.current_page = 1;
    this.last_page = 1;
    this.data.isLoadMore = true;
    this.setData({
      finished: false,
      dataList: []
    });
  },
  async getData() {
    this.init();
    let json = {};
    if(this.data.selectAgent) json.agent_id = this.data.selectAgent;

    let urlStr = app.getNetAddresss(this.activeOptions.api);
    let response = await app._postNetWork({
      url: urlStr,
      data: json
    });
    let { result, msg, data } = response.data;
    if(result == 0) return app.tips(msg);

    wx.setNavigationBarTitle({
      title: this.activeOptions.name
    });


    let setdata = {};
    setdata.info = data;
    setdata.dataList = data.reward_list.data;

    if(data.store) setdata.info.member = {
      nickname: data.store.store_name,
      avatar: data.store.thumb,
    };
    if(this.data.identity == 'clerk') {
      setdata.info.member.level_name = '店员';
    }
    if(data.agent) {
      // 判断data.agent数组里面 data.agent_id 对应索引
      setdata.selectAgent = data.agent[data.agent.findIndex(item => item.agent_id == data.agent_id)].agent_id;
      setdata.selectAgentName = data.agent[data.agent.findIndex(item => item.agent_id == data.agent_id)].name;
    }
    // if(!this.data.selectAgent && data.agent) setdata.selectAgent = data.agent[0].agent_id;
    this.current_page = data.reward_list.current_page;
    this.last_page = data.reward_list.last_page;
    if (this.current_page == this.last_page) {
      setdata.finished = true;
    }
    this.setData(setdata);
  },
  async getMoreData(){
    if (this.current_page >= this.last_page) {
      this.setData({finished: true});
      return;
    }
    this.data.isLoadMore = false;
    this.current_page = this.current_page + 1; 
    let json = {
      page: this.current_page
    };
    if(this.data.selectAgent) json.agent_id = this.data.selectAgent;

    let urlStr = app.getNetAddresss(this.activeOptions.api);
    let response = await app._postNetWork({
      url: urlStr,
      data: json,
    });
    let { result, msg, data } = response.data;
    this.data.isLoadMore = true;
    if(result == 0) return app.tips(msg);
    this.setData({
      dataList: this.data.dataList.concat(data.reward_list.data)
    });
  },
  toUrl(e) {
    let _data = e.currentTarget.dataset || {};
    this.wxRouterLink(_data);
  },
  wxRouterLink(_data) {
    let _Param = "";
    const { url, ...otherParam } = _data;
    Object.keys(otherParam).forEach(function (key) {
      if (otherParam[key] !== "") _Param += key + "=" + otherParam[key] + "&";
    });
    if (url === "") return;
    wx.navigateTo({
      url: url + "?" + _Param.slice(0, -1),
    });
  },
  onClickradio(event) {
    const { name } = event.currentTarget.dataset;
    this.setData({
      selectAgent: name,
    });
    this.getData();
  },
  onClickVal(e){
    let {name,val} = e.currentTarget.dataset;
    this.setData({
      [name]: val
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage() {

  // }
})